package com.lab.cloud.common.validation.util;


import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ForbiddenValidator implements ConstraintValidator<Forbidden, String> {
	private String[] forbiddenWords = { "admin", "fuck", "毛泽东" };

	@Override
	public void initialize(Forbidden constraintAnnotation) {

	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		value = StringUtils.trimToEmpty(value).toLowerCase();
		if (StringUtils.isEmpty(value)) {
			return true;
		}

		for (String word : forbiddenWords) {
			if (value.contains(word)) {
				return false;
			}
		}
		return true;
	}

}
